package com.example.agricultural_big_data.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.agricultural_big_data.util.AjaxResult;
import com.example.agricultural_big_data.util.LoginRequired;
import com.example.agricultural_big_data.util.QiniuUtils;
import com.example.agricultural_big_data.util.UUIDUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * ClassName:ImageController
 * Package:com.example.agricultural_big_data.controller
 * Description:
 *
 * @Data:2022/5/19 12:51
 */
@RestController
@CrossOrigin
@RequestMapping("/upload")
public class ImageController extends BaseController {

    @Autowired
    private QiniuUtils qiniuUtils;
//这个写在了配置文件中

//    @LoginRequired
    @PostMapping("/img")
    public AjaxResult upload(MultipartFile file) {
            String fileName = UUIDUtil.randomUUID() + "." + StringUtils.substringAfterLast(file.getOriginalFilename(), ".");
            boolean upload = qiniuUtils.upload(file, fileName);
            if (upload){
                return AjaxResult.success(QiniuUtils.url + fileName);
            }
            return AjaxResult.error(20001,"上传失败");

    }
}
